/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jpm.jroguefx;

import com.jpm.jroguefx.levels.Outside;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class JRogueFX extends Application implements Constants {

    private int adjSquareX, adjSquareY;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage stage) {

        final Group root = new Group();
        final Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
        final Scene scene = new Scene(root, Color.BLACK);

        final Canvas groundCanvas = new Canvas(screenBounds.getWidth(), screenBounds.getHeight());
        final Canvas selectionCanvas = new Canvas(screenBounds.getWidth(), screenBounds.getHeight());
        final Canvas playCanvas = new Canvas(screenBounds.getWidth(), screenBounds.getHeight());
        final Canvas uiCanvas = new Canvas(screenBounds.getWidth(), screenBounds.getHeight());
        final Canvas mapCanvas = new Canvas(screenBounds.getWidth(), screenBounds.getHeight());

        final GraphicsContext ground = groundCanvas.getGraphicsContext2D();
        final GraphicsContext selection = selectionCanvas.getGraphicsContext2D();
        final GraphicsContext playarea = playCanvas.getGraphicsContext2D();
        final GraphicsContext ui = uiCanvas.getGraphicsContext2D();
        final GraphicsContext map = mapCanvas.getGraphicsContext2D();

        final Outside level = new Outside();
        level.drawGround(ground);
        level.drawPlayArea(playarea);
        //level.drawMap(ui);

        uiCanvas.setOnKeyPressed(new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent arg0) {

            }

        });

        uiCanvas.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {

                if (adjSquareX > 0 && adjSquareX < level.getXlimit() && adjSquareY > 0 && adjSquareY < level.getYlimit()) {

                    Player.setLocX(adjSquareX);
                    Player.setLocY(adjSquareY);

                    //System.out.println(adjSquareX+","+adjSquareY);
                    ground.clearRect(0, 0, screenBounds.getWidth(), screenBounds.getHeight());
                    playarea.clearRect(0, 0, screenBounds.getWidth(), screenBounds.getHeight());

                    level.drawGround(ground);
                    level.drawPlayArea(playarea);

                    level.showMaskValues(adjSquareX, adjSquareY);

                    System.out.println(adjSquareX + "," + adjSquareY);
                }
            }
        });

        uiCanvas.setOnMouseMoved(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {

                int squareX = (int) Math.floor((event.getX() / (TILE_SIZE / 2)) + 0.5);
                int squareY = (int) Math.floor((event.getY() / (TILE_SIZE / 4)) + 0.5);

                double mouseX = squareX * (TILE_SIZE / 2);
                double mouseY = squareY * (TILE_SIZE / 4);

                if ((squareX + squareY) % 2 == 1) {

                    double deltaXRatio = event.getX() - (mouseX / (TILE_SIZE / 2));
                    double deltaYRatio = event.getY() - (mouseY / (TILE_SIZE / 4));

                    if (Math.abs(deltaXRatio) > Math.abs(deltaYRatio)) {
                        if (deltaXRatio > 0) {
                            squareX++;
                            mouseX += (TILE_SIZE / 2);
                        } else {
                            squareX--;
                            mouseX -= (TILE_SIZE / 2);
                        }

                    } else {

                        if (deltaYRatio > 0) {
                            squareY++;
                            mouseY += (TILE_SIZE / 4);
                        } else {
                            squareY--;
                            mouseY -= (TILE_SIZE / 4);
                        }
                    }
                }

                adjSquareX = ((squareX - squareY + 54) / 2) + Player.getLocX() - 25;
                adjSquareY = (-(-squareY - squareX) / 2) + Player.getLocY() - 25;

                selection.setStroke(Color.RED);
                selection.clearRect(0, 0, selectionCanvas.getWidth(), selectionCanvas.getHeight());
                selection.strokePolygon(new double[]{mouseX, mouseX + (TILE_SIZE / 2), mouseX, mouseX - (TILE_SIZE / 2)},
                        new double[]{mouseY + (TILE_SIZE / 4), mouseY, mouseY - (TILE_SIZE / 4), mouseY},
                        4);
            }
        });


        //ui.setFill(Color.GHOSTWHITE);
        //ui.fillRoundRect(10, 10, 200, screenBounds.getHeight() - 10, 10, 10);

        root.getChildren().add(groundCanvas);
        root.getChildren().add(selectionCanvas);
        root.getChildren().add(playCanvas);
        root.getChildren().add(uiCanvas);

        stage.setFullScreen(false);
        stage.setScene(scene);
        stage.show();

    }
}
